﻿// ReSharper disable CheckNamespace

using System.Globalization;
using System.Net;
using System.IO;
using System;

namespace WindowsPhoneCommands
// ReSharper restore CheckNamespace
{
    [Serializable]
    public class UserSettings
    {

        #region Singleton

        private static UserSettings _current;

        public static UserSettings Current
        {
            get { return _current ?? (_current = Load()); }
        }


        internal static void Save()
        {
            var filename = Path.Combine(Common.FolderData(), Constants.SettingsFileName);

            using (var stream = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
            {
                var formatter = new System.Xml.Serialization.XmlSerializer(new UserSettings().GetType());
                formatter.Serialize(stream, _current);
            }
        }

        internal static void ReLoad()
        {
            _current = Load();
        }

        private static UserSettings Load()
        {
            var filename = Path.Combine(Common.FolderData(), Constants.SettingsFileName);

            var result = new UserSettings();
            if (File.Exists((filename)))
            {
                using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var formatter = new System.Xml.Serialization.XmlSerializer(new UserSettings().GetType());
                    result = formatter.Deserialize(stream) as UserSettings;
                }
            }
            return result;
        }

        #endregion


        private string _folderLanguages;
        private string _neutralResource;
        private string _xamlClassHelper;
        private string _urlGoogle;
        private string _findjsonGoogle;
        // ReSharper disable InconsistentNaming
        private string _md5Password;
        // ReSharper restore InconsistentNaming


        public string ProxyAddressAndPort { get; set; }
        public string ProxyUserName { get; set; }
        public string ProxyUserDomain { get; set; }
        public string ProxyPassword { get; set; }

        public EngineTranslate EngineTranslate { get; set; }
        public string BingClientId { get; set; }
        public string BingSecretCode { get; set; }
        public string Md5Password
        {
            get { return _md5Password ?? (_md5Password = EncryptDecryptMd5.EncryptString("1234", Constants.GuidRef)); } 
            set { _md5Password = value; }
        }
        public string UrlGoogle
        {
            get { return _urlGoogle ?? (_urlGoogle = EncryptDecryptMd5.EncryptString(Constants.UrlTransGoogle,  EncryptDecryptMd5.DecryptString(Md5Password,Constants.GuidRef)));}
            set { _urlGoogle = value; }
        }
        public string FindjsonGoogle
        {
            get { return _findjsonGoogle ?? (_findjsonGoogle = EncryptDecryptMd5.EncryptString(Constants.FindJsonGoogle,  EncryptDecryptMd5.DecryptString(Md5Password,Constants.GuidRef)));}
            set { _findjsonGoogle = value; }
        }
        public bool PromptPwd { get; set; }
        public string FolderLanguages
        {
            get { return _folderLanguages ?? (_folderLanguages = "LanguageResources"); }
            set { _folderLanguages = value; }
        }
        public string NeutralResource
        {
            get { return _neutralResource ?? (_neutralResource = "AppLngResource.resx"); }
            set { _neutralResource = value; }
        }
        public string XamlClassHelper
        {
            get { return _xamlClassHelper ?? (_xamlClassHelper = "LocalizedStrings"); }
            set { _xamlClassHelper = value; }
        }

        public bool HasBingCredencial
        { 
            get
            {
                var result = EncryptDecryptMd5.DecryptString(BingClientId, EncryptDecryptMd5.DecryptString(Md5Password, Constants.GuidRef));
                if (string.IsNullOrEmpty(result))
                {
                    return false;
                }
                result = EncryptDecryptMd5.DecryptString(BingSecretCode, EncryptDecryptMd5.DecryptString(Md5Password, Constants.GuidRef));
                if (string.IsNullOrEmpty(result))
                {
                    return false;
                }
                return true;
            }
        }

        public bool HasGoogleCredencial
        {
            get
            {
                var result = EncryptDecryptMd5.DecryptString(UrlGoogle, EncryptDecryptMd5.DecryptString(Md5Password, Constants.GuidRef));
                if (string.IsNullOrEmpty(result))
                {
                    return false;
                }
                result = EncryptDecryptMd5.DecryptString(FindjsonGoogle, EncryptDecryptMd5.DecryptString(Md5Password, Constants.GuidRef));
                if (string.IsNullOrEmpty(result))
                {
                    return false;
                }
                return true;
            }
        }
  
        public WebProxy Proxy
        {
            get
            {
                WebProxy proxy;
                var pwd = EncryptDecryptMd5.EncryptString(ProxyPassword,EncryptDecryptMd5.DecryptString(UserSettings.Current.Md5Password, Constants.GuidRef));
                if (!string.IsNullOrEmpty(ProxyAddressAndPort) && !string.IsNullOrEmpty(ProxyUserName) && !string.IsNullOrEmpty(pwd))
                {
                    proxy = ProxyUserDomain != null ?
                        new WebProxy(ProxyAddressAndPort, true, null,
                            new NetworkCredential(ProxyUserName, pwd, ProxyUserDomain)) { UseDefaultCredentials = false } :
                        new WebProxy(ProxyAddressAndPort, true, null,
                            new NetworkCredential(ProxyUserName, pwd)) { UseDefaultCredentials = false };
                }
                else
                {
                    proxy = new WebProxy { UseDefaultCredentials = true };
                }
                return proxy;
            }
        }

    }
}
